package com.ninetaildemonfox.zdl.socialproject.mvp.presenter;

import android.support.v7.app.AppCompatActivity;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.DateDetailsContract;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/8/15
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class DateDetailsPresenter {
    public AppCompatActivity me;
    private DateDetailsContract contract;

    public DateDetailsPresenter(AppCompatActivity me, DateDetailsContract contract) {
        this.me = me;
        this.contract = contract;
    }

    public void orderInfo(String token, String order_id, int type) {
        contract.showLoading();
        Model
                .getInstance()
                .getUserApi()
                .orderInfo(token, order_id)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                        contract.ateSuccess(data, type);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    public void dynamicInfo(String token, String order_id, int type) {
        contract.showLoading();
        Model
                .getInstance()
                .getUserApi()
                .dynamicInfo(token, order_id, "1")
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                        contract.ateSuccess(data, type);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    /**
     * 删除动态
     */
    public void delDynamic(String token, String order_id) {
        Model.getInstance().getUserApi().delDynamic(token, order_id)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        ToastUtil.show(map.get("message"));
                        contract.delDynamic();
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });

    }

    /**
     * 取消报名
     */
    public void endOrder(String token, String order_id, final int item) {
        HttpRequest.POST(me, "Order/endOrder"
                , new Parameter()
                        .add("token", token)
                        .add("order_id", order_id)
                , new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        if (error != null) {
                            return;
                        }
                        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                        if (map.get("code").equals("1")) {
//                            contract.addOrderGood(item, 2);
                            contract.endBaoming();
                            return;
                        }
                        ToastUtil.show(map.get("message"));

                    }
                });

    }


}
